﻿@model HAProxyModel
@{
    Layout = "~/Views/Shared/Master.cshtml";
    this.SetPageTitle("HAProxy");
    var allGroups = Module.Groups;
    this.SetTopNodes(Module.Groups, "HAProxy Groups", Model.SelectedGroup);
}
@section head {
@if (Model.IsInstanceFilterable)
{
    <script>
        $(function() {
            Status.HAProxy.init({
                refresh: @(Model.Refresh ? 5 : 0), 
                group: '@(Model.SelectedGroup != null ? Model.SelectedGroup.Name : "")', 
                proxy: '@(Model.WatchProxy)'
            });
        });
    </script>
}
}
@functions
{
    string TabLink(HAProxyModel.Views view, string action, string name)
    {
        <li class="@(Model.View == view ? "active" : null)"><a href="@Url.Action(action)">@name</a></li>
        return null;
    }
}
<ul class="nav nav-tabs nav-tabs-right">
    @TabLink(HAProxyModel.Views.Dashboard, nameof(HAProxyController.Dashboard), "dashboard")
</ul>
<div class="js-refresh" data-name="haproxy-header">
    @if (Model.IsInstanceFilterable)
    {
        var allProxies = Module.GetAllProxies();
        <ul class="badge-link-list small">
            <li class="@(Model.SelectedGroup == null ? "active" : null)">
                <a href="?">All</a>
                <span class="badge">@allProxies.Count.ToComma()</span>
            </li>
            @foreach (var g in allGroups)
            {
                var count = allProxies.Count(e => e.Instance.Group.Name == g.Name && e.Servers.Any());
                var lastPoll = g.Instances.Max(i => i.Proxies.LastPoll);
                var lastSuccess = g.Instances.Max(i => i.Proxies.LastSuccess);
                var erroring = g.Instances.FirstOrDefault(i => i.Proxies.ErrorMessage.HasValue());
                <li class="@(g == Model.SelectedGroup ? "active" : null)" data-name="@g.Name" title="@g.Name:
Last Poll: @(lastPoll?.ToRelativeTime() ?? "Not yet polled")
Last Success: @(lastSuccess?.ToRelativeTime() ?? "never")@if (erroring != null) {<text>
Error: @(erroring.Proxies.ErrorMessage)</text>
}">
                    <a href="?group=@g.Name.UrlEncode()" class="@g.TextClass()">@g.Name</a>
                    <span class="badge alert-@g.RawClass()">@count.ToComma()</span>
                </li>
            }
        </ul>
    }
    
    <partial name="PollInfo" model="@(new Opserver.Views.PollInfoModel { Nodes = allGroups.SelectMany(g => g.Instances), Name = "HAProxy" })" />
</div>
@RenderBody()